package dp.mobile.game;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Point;
import dp.mobile.entity.saveable.SaveablePlayer;
import dp.mobile.glwrapper.GLTexture;
import dp.mobile.glwrapper.GLTiledTexture;
import dp.mobile.glwrapper.GameRenderer;
import dp.mobile.level.Level;
import dp.mobile.system.Utilities;

public class Player {
	public Player() {
		mWave			= 0;
		mLevel			= null;
		mElectricity	= 0;
		mComponents		= new int[]{0, 0, 0};
		mScore			= 0;
		mLife			= 0;
		mBonus			= 1;
		mCursorType		= 0;
		mSelectedTile 	= new Point(-1, -1);
		mHasWon			= false;
		
		GLTexture texture = GameRenderer.instance().allocateTexture(Utilities.INTERFACE_FOLDER + "/cursor.png", true);
		final int col 	= texture.Width / Utilities.TILE_WIDTH;
		mCursor			= new GLTiledTexture[col];
		
		for (int x = 0; x < col; ++x) {
			final float left 	= (float)x 		/ (float)col;
			final float right 	= (float)(x + 1)/ (float)col;
			final float top		= 0;
			final float bottom	= 1;
			
			float[] crop = new float[] {
					left,	bottom,
					right,	bottom,
					left,	top,
					right,	top
				};
			
			mCursor[x] = new GLTiledTexture(texture, crop);
		}
	}
	
	public Player(Level data , int imageIndex) {
		//Initialize
		this();
		mLevel			= data;
		mElectricity	= mLevel.getGold();
		mLife			= mLevel.getLife();
		mComponents		= mLevel.getComponents();
		mImageIndex		= imageIndex;
		int extra		= mWave >= mLevel.getWaveLength() ? 0 : 1;
		
		// Create images
		String Gold	= String.valueOf(mElectricity);
		// make string gold since it's goal
		if (mLevel.getGoldGoal() > 0) Gold += "/" + mLevel.getGoldGoal();
		String waves = (mWave + extra) + "/" + mLevel.getWaveLength() + " wave";
	}
	
	public int getWave()        	{	return mWave;		}
	public int getScore()			{	return mScore;		}
	public int getElectricity()		{	return mElectricity;}
	public int getLife()			{	return mLife;		}
	public boolean hasWon()			{	return mHasWon;		}
	void setWon(boolean won)		{	mHasWon	= won;		}
	public int getComponents(int num){	return mComponents[num];	}
	
	public void nextWave() {
		//Increment wave
		++mWave;
		++mBonus;
		int extra = mWave >= mLevel.getWaveLength() ? 0 : 1;
	}
	
	public void decrementLife() {
		//Reduce life
		mLife--;
		if (mListener != null) {
			mListener.onLifeChange(mLife);
		}
	}
	
	public void addElectricity(int addition) {
		//Skip if 0
		if (addition == 0) return;
		
		//Set gold
		mElectricity += addition;
		String Gold	= String.valueOf(mElectricity);
		if (mLevel.getGoldGoal() > 0) Gold += "/" + mLevel.getGoldGoal();
		if (mListener != null) {
			mListener.onElectricityChange(mElectricity);
		}
	} 
	
	public void addComponent(int num, int addition) {
		if (addition == 0) return;
		
		mComponents[num] += addition;
		if (mListener != null) {
			mListener.onComponentChange(mComponents);
		}
	}
	
	public void addScore(int score) {
		mScore += score * mBonus;
	}
	
	public void resetBonus() {
		mBonus = 1;
	}
	
	public boolean isSelectedTileValid() {
		return mSelectedTile.x != -1 && mSelectedTile.y != -1;
	}
	
	public Point getSelectedTile() {
		return mSelectedTile;
	}
	
	public void setSelectedTile(int x, int y) {
		mSelectedTile.set(x, y);
	}
	
	public void setSelectedTileInvalid() {
		mSelectedTile.set(-1, -1);
	}
	
	public void setCursorType(int type) {
		mCursorType = type;
	}
	
	public void drawCursor(GL10 gl){
		if (!isSelectedTileValid()) return;
		
		final int x = mSelectedTile.x * Utilities.TILE_WIDTH;
		final int y = mSelectedTile.y * Utilities.TILE_HEIGHT;
		
		GameRenderer.instance().draw(gl, mCursor[mCursorType].Texture, mCursor[mCursorType].Crop, x, y, Utilities.TILE_WIDTH, Utilities.TILE_HEIGHT, Utilities.CURSOR_DEPTH);
	}
	
	public SaveablePlayer createSaveable() {
		SaveablePlayer retval = new SaveablePlayer();
		
		retval.mBonus = mBonus;
		retval.mComponents = mComponents.clone();
		retval.mElectricity = mElectricity;
		retval.mLife = mLife;
		retval.mScore = mScore;
		retval.mWave = mWave;
		
		return retval;
	}
	
	public void initFromSaveable(SaveablePlayer player) {
		mBonus = player.mBonus;
		mComponents = player.mComponents.clone();
		mElectricity = player.mElectricity;
		mLife = player.mLife;
		mScore = player.mScore;
		mWave = player.mWave;
	}
	
	public String getSummary() {
		String retval = "";
		
		retval += (mLife <= 0) ? "0" : "1"; retval += ";";
		retval += mScore + ";";
		retval += mElectricity + ";";
		retval += mBonus + "#";
		
		return retval;
	}
	
	public int getImageIndex() {
		return mImageIndex;
	}
	
	public void setImageIndex(int index) {
		mImageIndex = index;
	}

	public void setGameListener(GameEventListener listener) {
		mListener = listener;
	}

	public void removeGameListener() {
		mListener = null;
	}
	
	//Data
	protected int	mElectricity;
	protected int[] mComponents;
	protected int	mLife;
	protected int 	mBonus;
	protected int	mScore;
	protected int	mWave;
	protected Level mLevel;
	private boolean mHasWon;
	private int mImageIndex;
	
	// game stuffs
	private Point mSelectedTile;
	private int mCursorType;
	
	//String images
	protected GLTiledTexture mGoldString;
	protected GLTiledTexture mLifeString;
	protected GLTiledTexture mWaveString;
	
	//Panel images
	protected GLTiledTexture[] mCursor;
	protected GameEventListener mListener;
}
